﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using XinYuanOfficeWinForm.RoleServiceReference;
using System.Reflection;
using XinYuan.Office.Models;
using XinYuanOfficeWinForm.Admin.Code;

namespace XinYuanOfficeWinForm.Admin.Permission
{
    public partial class FrmRightsGroup : Form
    {
        private ActionType actionType;
        RoleServiceClient roleClient = new RoleServiceClient();
        public FrmRightsGroup()
        {
            InitializeComponent();
            dgvRoleList.AutoGenerateColumns = false;
        }

        private void FrmRightsGroup_Load(object sender, EventArgs e)
        {
            lblRoleId.Text = "";
            lblRoleId.Visible = false;
            Form f = null;
            if (TraverseForm("FrmMain", ref f))
            {
                TraverseMenu(f);
            }
            LoadAllRole();
        }
        /// <summary>
        /// 加载角色列表
        /// </summary>
        void LoadAllRole()
        {
            DataSet ds = roleClient.GetRoles();
            dgvRoleList.DataSource = ds.Tables[0];
        }

        private void dgvRoleList_Click(object sender, EventArgs e)
        {
            if (dgvRoleList.CurrentRow != null)
            {
                this.actionType = ActionType.Update;
                txtRoleName.Text = dgvRoleList.CurrentRow.Cells["RoleName"].Value.ToString().Trim();
                txtRoleDes.Text = dgvRoleList.CurrentRow.Cells["RoleDescription"].Value.ToString().Trim();
                txtRoleName.Enabled = txtRoleDes.Enabled = false;
                lblRoleId.Text = dgvRoleList.CurrentRow.Cells["RoleId"].Value.ToString().Trim();//角色编号
                string func = roleClient.GetFunctionById(Convert.ToInt32(lblRoleId.Text));
                CheckFunction(func);
            }
        }

        private void tsbtnAddRole_Click(object sender, EventArgs e)
        {
            txtRoleDes.Text = txtRoleName.Text = "";
            txtRoleName.Enabled = txtRoleDes.Enabled = true;
            actionType = ActionType.Insert;
        }

        private void tsmiEditRole_Click(object sender, EventArgs e)
        {
            txtRoleDes.Enabled = txtRoleName.Enabled = true;
        }
        bool TraverseForm(string str, ref Form f)
        {
            Assembly a = Assembly.LoadFile(Application.ExecutablePath);
            Type[] types = a.GetTypes();
            foreach (Type t in types)
            {
                Console.WriteLine(t.Name);
                if (t.BaseType != null && t.BaseType.Name == "Form")
                {
                    f = (Form)Activator.CreateInstance(t, true);
                    if (f != null)
                    {
                        if (f.Name == str)
                        {
                            return true;
                        }
                    }
                }
            }
            return false;
        }
        void TraverseMenu(Control control)
        {
            Admin.Code.FormMenuList.AllMenu.Clear();
            foreach (Control c in control.Controls)
            {
                if (c is MenuStrip)
                {
                    MenuStrip menu = c as MenuStrip;
                    //遍历一级菜单
                    for (int i = 0; i < menu.Items.Count; i++)
                    {
                        ToolStripMenuItem m = menu.Items[i] as ToolStripMenuItem;
                        Admin.Code.FormMenu formMenu = new Code.FormMenu();
                        formMenu.MenuID = m.Name;
                        formMenu.EnableState = m.Enabled;
                        formMenu.MenuParentID = "";
                        formMenu.MenuName = m.Text;
                        Admin.Code.FormMenuList.AddMenu(formMenu);
                        tvwRightView.Nodes.Add(menu.Items[i].Name, menu.Items[i].Text);
                        //遍历二级菜单
                        for (int j = 0; j < m.DropDownItems.Count; j++)
                        {
                            Admin.Code.FormMenu subMenu = new Code.FormMenu();
                            if (m.DropDownItems[j].GetType().Name == "ToolStripSeparator")
                            {
                                tvwRightView.Nodes[i].Nodes.Add(m.DropDownItems[j].Name, "-------------");
                                tvwRightView.Nodes[i].Nodes[m.DropDownItems[j].Name].ForeColor = Color.Red;
                                tvwRightView.Nodes[i].Nodes[m.DropDownItems[j].Name].ToolTipText = "分割线";
                                subMenu.MenuName = "-------------";
                            }
                            else
                            {
                                tvwRightView.Nodes[i].Nodes.Add(m.DropDownItems[j].Name, m.DropDownItems[j].Text);
                                tvwRightView.Nodes[i].Nodes[m.DropDownItems[j].Name].ToolTipText = m.DropDownItems[j].Text;
                                subMenu.MenuName = m.DropDownItems[j].Text;
                            }
                            subMenu.MenuParentID = formMenu.MenuID;
                            subMenu.MenuID = m.DropDownItems[j].Name;
                            subMenu.EnableState = m.DropDownItems[j].Enabled;
                            Admin.Code.FormMenuList.AddMenu(subMenu);
                            Console.WriteLine(m.DropDownItems[j].Name + "" + m.DropDownItems[j].Enabled);
                        }

                    }
                }
            }
            tvwRightView.ExpandAll();
        }
        //保存
        private void tsbtnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (lblRoleId.Text.Trim() == "")
                { if (this.actionType == ActionType.Update)return; }
                string funcStr = string.Empty;
                AppendFunctions(ref funcStr);
                Role role = new Role();
                role.Id = Convert.ToInt32(lblRoleId.Text);
                role.Name = txtRoleName.Text.Trim();
                role.Descrition = txtRoleDes.Text.Trim();
                role.FunctionStr = funcStr;
                int i = 0;
                if (this.actionType == ActionType.Update)
                {
                    i = roleClient.Update(role);
                }
                else if (this.actionType==ActionType.Insert)
                {
                    i = roleClient.Insert(role);
                }
                if (i == 1)
                {
                    MessageBox.Show("保存成功!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("保存失败!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        private void AppendFunctions(ref string funcStr)
        {
            for (int i = 0; i < tvwRightView.Nodes.Count; i++)
            {
                if (tvwRightView.Nodes[i].Checked)
                {
                    funcStr += tvwRightView.Nodes[i].Name;
                }
                for (int j = 0; j < tvwRightView.Nodes[i].Nodes.Count; j++)
                {
                    if (tvwRightView.Nodes[i].Nodes[j].Checked)
                    {
                        funcStr += tvwRightView.Nodes[i].Nodes[j].Name;
                    }
                }
            }
        }

        private void CheckFunction(string function)
        {
            for (int i = 0; i < tvwRightView.Nodes.Count; i++)
            {
                tvwRightView.Nodes[i].Checked = false;
                if (function.Contains(tvwRightView.Nodes[i].Name))
                {
                    tvwRightView.Nodes[i].Checked = true;
                }
                for (int j = 0; j < tvwRightView.Nodes[i].Nodes.Count; j++)
                {
                    tvwRightView.Nodes[i].Nodes[j].Checked = false;
                    if (function.Contains(tvwRightView.Nodes[i].Nodes[j].Name))
                    {
                        tvwRightView.Nodes[i].Nodes[j].Checked = true;
                    }
                }
            }
        }
    }
}
